import wxsdk from 'weixin-js-sdk';
import { getSignature } from '@/api/shop';

/**
 * 微信验签
 * @param {*} config
 */
export function wxChatSignature(config) {
    return new Promise((resolve, reject) => {
        let info = {
            debug: false, // 调试，发布的时候改为false
            ...config,
            jsApiList: [
                // 'checkJsApi',
                // 'onMenuShareAppMessage', // “分享给朋友”
                // 'onMenuShareTimeline', // “分享到朋友圈”
                // 'updateAppMessageShareData', // “分享给朋友”
                // 'updateTimelineShareData', // “分享到朋友圈”
                'chooseWXPay', // 微信支付
                // 'openLocation',
                // 'getLocation',
            ],
        };
        wxsdk.error((err) => {
            console.log('err', err);
            reject(err);
        });
        wxsdk.ready(() => {
            console.log('微信js-sdk验签成功');
            resolve(wxsdk);
        });

        wxsdk.config(info);
    });
}

export function initJssdk() {
    return new Promise((resolve, reject) => {
        let systemInfo = uni.getSystemInfoSync();
        const currUrl = systemInfo.platform === 'ios' ? window.enterUrl : window.location.href;
        getSignature({ shareUrl: encodeURI(currUrl) })
            .then((config) => {
                let info = {
                    debug: false, // 调试，发布的时候改为false
                    ...config,
                    jsApiList: [
                        'checkJsApi',
                        // 'onMenuShareAppMessage', // “分享给朋友”
                        // 'onMenuShareTimeline', // “分享到朋友圈”
                        // 'updateAppMessageShareData', // “分享给朋友”
                        // 'updateTimelineShareData', // “分享到朋友圈”
                        'chooseWXPay', // 微信支付
                        // 'openLocation',
                        'getLocation',
                    ],
                };
                console.log('验签info', info);
                wxsdk.config(info);
            })
            .catch((err) => {
                reject(err);
            });

        wxsdk.error((err) => {
            console.log('err', err);
            reject(err);
        });
        wxsdk.ready(() => {
            console.log('微信js-sdk验签成功');
            resolve(wxsdk);
        });
    });
}

/**
 1. 微信 浏览器 web 支付
 2. @param {Object} orderInfo
 */
export function wxChatWebPay(orderInfo) {
    orderInfo['timestamp'] = orderInfo.timeStamp;
    return new Promise((resolve, reject) => {
        wxChatSignature(orderInfo).then(() => {
            wxsdk.chooseWXPay({
                ...orderInfo,
                success: (res) => {
                    console.log('支付成功');
                    resolve(res);
                },
                fail: (err) => {
                    // 支付失败
                    console.log('支付失败');
                    reject(false);
                },
                cancel: (err) => {
                    // 支付取消
                    console.log('支付取消');
                    reject(false);
                },
            });
        });
    });
}

/**
 * 获取定位
 */
export function getLocation() {
    return new Promise((resolve, reject) => {
        console.log('开始initJssdk');
        initJssdk()
            .then((res) => {
                console.log('initJssdk成功');
                wxsdk.getLocation({
                    type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    success: (res) => {
                        resolve(res);
                    },
                    fail: (err) => {
                        reject(err);
                    },
                });
            })
            .catch((err) => {
                console.log('initJssdk失败');
                reject(err);
            });
    });
}
